﻿using System.Collections.Generic;
using System.Linq;
using ESoft.Core.Data.Service;
using ESoft.Core.Utils.Extensions;
using ESoft.Core.Web.Common.Filters;
using ESoft.Portal.Areas.ContentManager.Data.Service;
using ESoft.Portal.Data.Domain.Entity;
using Microsoft.AspNetCore.Mvc;

namespace ESoft.Portal.Controllers
{
    [SkipUserAuthorize]
    public class HomeController : Controller
    {
        private readonly IBaseService _baseService;
        private readonly IChannelService _channelService;

        public HomeController(IBaseService baseService, IChannelService channelService)
        {
            _baseService = baseService;
            _channelService = channelService;
        }


        public IActionResult Index(string channelId="")
        {
            var channelList = _baseService.QueryEntities<CContentChannelEntity>().OrderBy(x => x.Sort).ThenBy(x => x.CreateDatetime).ToList();
            ViewBag.channelList = channelList;
            List<CContentArticleEntity> articleList = _baseService.QueryEntities<CContentArticleEntity>(x=>x.ChannelId== channelId||channelId== string.Empty).OrderByDescending(x => x.CreateDatetime).ToList();

            foreach (var article in articleList)
            {
                if (article.ChannelId.IsNotNullOrWhiteSpace())
                {
                    var cc = _baseService.Find<CContentChannelEntity>(article.ChannelId);
                    if (cc != null)
                        article.Channel = cc;
                }

                if (article.RichTextId.IsNotNullOrWhiteSpace())
                {
                    var richText = _baseService.Find<CRichTextEntity>(article.RichTextId);
                    if (richText != null)
                        article.RichText = richText;
                }
            }

            return View(articleList);
        }
    }
}
